package com.platform.web.controller.report;

import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.data.domain.ValveData;
import com.platform.data.service.IValveDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 阀门统计曲线图
 *
 * @author wangqi
 * @date 2020-08-04
 */
@Controller
@RequestMapping("/report/valve")
public class ValveReportController extends BaseController {

    private String prefix = "report/valve";

    @Autowired
    private IValveDataService valveDataService;

    @RequiresPermissions("report:valve:view")
    @GetMapping(value = { "", "/{valveId}" })
    public String valve(@PathVariable(value = "valveId", required = false) String valveId, ModelMap mmap)
    {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        ca.add(Calendar.MONTH, -1);
        Date resultDate = ca.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        mmap.put("beginTime", dateFormat.format(resultDate));
        mmap.put("endTime", dateFormat.format(new Date()));
        mmap.put("valveId", valveId);

        return prefix + "/valve";
    }

    /**
     * 室温变化曲线图
     */
    @PostMapping("/valveSummary")
    @ResponseBody
    public AjaxResult valveSummary(ValveData valveData)
    {
        if(valveData.getParams().get("beginTime").equals("") && valveData.getParams().get("endTime").equals("")){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            valveData.getParams().put("beginTime", simpleDateFormat.format(new Date()));
            valveData.getParams().put("endTime", simpleDateFormat.format(new Date()));
        }
        if(valveData.getValveId() != null && !valveData.getValveId().equals("")){
            valveData.setLocationId(null);
        }
        List<ValveData> valveDataList  = valveDataService.selectValveDatarList(valveData);
        return AjaxResult.success(valveDataList);
    }
}
